<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="标题内容" prop="title">
      <el-input v-model="dataForm.title" placeholder="标题内容"></el-input>
    </el-form-item>
    <el-form-item label="话题文本信息" prop="content">
      <el-input v-model="dataForm.content" placeholder="话题文本信息"></el-input>
    </el-form-item>
    <el-form-item label="收藏数量" prop="starCount">
      <el-input v-model="dataForm.starCount" placeholder="收藏数量"></el-input>
    </el-form-item>
    <el-form-item label="点击数量" prop="clickCount">
      <el-input v-model="dataForm.clickCount" placeholder="点击数量"></el-input>
    </el-form-item>
    <el-form-item label="评论数量" prop="commentCount">
      <el-input v-model="dataForm.commentCount" placeholder="评论数量"></el-input>
    </el-form-item>
    <el-form-item label="创建人信息（关联用户表）" prop="createUserId">
      <el-input v-model="dataForm.createUserId" placeholder="创建人信息（关联用户表）"></el-input>
    </el-form-item>
    <el-form-item label="逻辑删除，0 没删除，1 已删除，默认为0" prop="isDelete">
      <el-input v-model="dataForm.isDelete" placeholder="逻辑删除，0 没删除，1 已删除，默认为0"></el-input>
    </el-form-item>
    <el-form-item label="受欢迎程度权重（推荐算法）" prop="weight">
      <el-input v-model="dataForm.weight" placeholder="受欢迎程度权重（推荐算法）"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="修改时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="修改时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          title: '',
          content: '',
          starCount: '',
          clickCount: '',
          commentCount: '',
          createUserId: '',
          isDelete: '',
          weight: '',
          createTime: '',
          updateTime: ''
        },
        dataRule: {
          title: [
            { required: true, message: '标题内容不能为空', trigger: 'blur' }
          ],
          content: [
            { required: true, message: '话题文本信息不能为空', trigger: 'blur' }
          ],
          starCount: [
            { required: true, message: '收藏数量不能为空', trigger: 'blur' }
          ],
          clickCount: [
            { required: true, message: '点击数量不能为空', trigger: 'blur' }
          ],
          commentCount: [
            { required: true, message: '评论数量不能为空', trigger: 'blur' }
          ],
          createUserId: [
            { required: true, message: '创建人信息（关联用户表）不能为空', trigger: 'blur' }
          ],
          isDelete: [
            { required: true, message: '逻辑删除，0 没删除，1 已删除，默认为0不能为空', trigger: 'blur' }
          ],
          weight: [
            { required: true, message: '受欢迎程度权重（推荐算法）不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '修改时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/circle/topic/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.title = data.topic.title
                this.dataForm.content = data.topic.content
                this.dataForm.starCount = data.topic.starCount
                this.dataForm.clickCount = data.topic.clickCount
                this.dataForm.commentCount = data.topic.commentCount
                this.dataForm.createUserId = data.topic.createUserId
                this.dataForm.isDelete = data.topic.isDelete
                this.dataForm.weight = data.topic.weight
                this.dataForm.createTime = data.topic.createTime
                this.dataForm.updateTime = data.topic.updateTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/circle/topic/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'title': this.dataForm.title,
                'content': this.dataForm.content,
                'starCount': this.dataForm.starCount,
                'clickCount': this.dataForm.clickCount,
                'commentCount': this.dataForm.commentCount,
                'createUserId': this.dataForm.createUserId,
                'isDelete': this.dataForm.isDelete,
                'weight': this.dataForm.weight,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
